package cn.zjj.system.service.impl;

import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.system.domain.Menu;
import cn.zjj.system.mapper.MenuMapper;
import cn.zjj.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Override
    public List<Menu> menuTree() {
        List<Menu> menus = menuMapper.selectAll();
        HashMap<Long, Menu> map = new HashMap<>();
        //获取所有的菜单和其id 放入键值对中,方便后续操作
        for (Menu menu : menus) {
            map.put(menu.getId(),menu);
        }
        //无极树集合
        List<Menu> menuTree = new ArrayList<>();
        for (Menu menu : menus) {
            if(menu.getParent_id()==null){
                menuTree.add(menu);
            }else{
                Long parent_id = menu.getParent_id();
                //根据父菜单id找到父菜单
                Menu menuParent = map.get(parent_id);
                //给父菜单的子集合赋值
                menuParent.getChildren().add(menu);
            }
        }
        return menuTree;
    }
}
